<?php
	include 'include/function.php';

function do_register($reg_name,$reg_pwd,$reg_nick,$reg_lang) {
	//验证码
	if(!check_checkcode())return _d('err_checkcode');
	//用户名不能为空，在javascript中检测过了
	//密码长度
	if(strlen($reg_pwd)!=32)return "Please make sure that you have enabled the Javascript.";
	//判断重名
	$query="select name from user where name='$reg_name'";
	$ret=oj_query($query);
	$row=mysql_fetch_row($ret);
	if(!empty($row[0]))return "User " . stripslashes($_POST['name']) . " already exsist.";
	//注册
	$query='insert into user (name, password, reg_time, nick_name, language_display)'
		. " values ('$reg_name', '$reg_pwd', now(), '$reg_nick', $reg_lang)";
	$ret=oj_query($query);
	
	show_header(_d('title_register'));
	echo '<center><h1>Register success</h1></center>';
	include 'include/footer.php';
	die();
}
	
	if(isset($_POST['name'])) {
		$reg_name    = get_to_mysql($_POST['name']);
		$reg_pwd     = get_to_mysql($_POST['pwd']);
		$reg_nick    = get_to_mysql($_POST['nick']);
		
		$reg_msg=do_register($reg_name,$reg_pwd,$reg_nick,$_user_lang);
	}
	
	show_header(_d('title_register'));
	include_once 'include/form.php';
		
	if(isset($reg_msg))
		echo '<center><h1>' . str_to_html($reg_msg) . '</h1></center>';
	
	echo '<form action="register.php" method="post" onsubmit="return check_reg()">';
	draw_simple_form_fillin(array(
		'name'=>array('input',_d('user_id'),$reg_name,' id="reg_name" maxlength="32" '),
		'pwd'=>array('input',_d('user_password'),'',' id="reg_pwd" type="password" '),
		'pwd2'=>array('input',_d('user_password_repeat'),'',' id="reg_pwd2" type="password" '),
		'nick'=>array('input',_d('user_nick'),$reg_nick,' maxlength="100" '),
		'check'=>array('check',_d('check_code')),
		'submit'=>array('submit','',_d('user_register'))
	));
	echo '</form>';
?>
<script src="include/md5.js"></script>
<script language="javascript">
function check_reg() {
	var usr = document.getElementById('reg_name');
	if (usr.value.length <= 0) {
		alert("Please input your User ID.");
		return false;
	}
	var ps = document.getElementById('reg_pwd');
	var ps2 = document.getElementById('reg_pwd2');
	if (ps.value.length <= 0 || ps2.value.length <= 0) {
		alert("Please input and repeat your Password.");
		return false;
	}
	if(ps.value != ps2.value){
		alert("Password do not match.");
		return false;
	}
	ps.value = MD5(ps.value);
	ps2.value = MD5(ps2.value);
	return true;
}
</script>
<?php
	
	include 'include/footer.php';
?>

